package com.dopies.bsdmall.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.dopies.BeeFramework.activity.BaseActivity;
import com.dopies.BeeFramework.model.BusinessResponse;
import com.dopies.bsdmall.R;
import com.dopies.bsdmall.adapter.RecommendAdapter;
import com.dopies.bsdmall.model.ProtocolConst;
import com.dopies.bsdmall.model.RecommendUsersModel;
import com.external.androidquery.callback.AjaxStatus;

public class RecommendUsersActivity extends BaseActivity implements
		BusinessResponse {
	private TextView title;
	private ListView listView;
	private RecommendUsersModel recommendUsersModel;
	private RecommendAdapter spinnerAdapter;
	private SharedPreferences shared;

	private String user_id;
	private String parent_id;
	private String user_name;
	private int i = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recommend_users);
		title = (TextView) findViewById(R.id.recommend_title);
		listView = (ListView) findViewById(R.id.recommend_list);
		Resources resource = getBaseContext().getResources();
		String scoun = resource.getString(R.string.recommend_users);
		title.setText(scoun);
		shared = this.getSharedPreferences("userInfo", 0);

		recommendUsersModel = new RecommendUsersModel(this);
		recommendUsersModel.addResponseListener(this);
		String uid = shared.getString("uid", "");
		recommendUsersModel.getRecommendList(uid, i);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (i == 12)
					return;
				parent_id = recommendUsersModel.recommendList.get(position).user_id
						+ "";
				user_name = recommendUsersModel.recommendList.get(position).user_name;

				recommendUsersModel.getRecommendList(parent_id, i);
			}
		});
	}

	private void setUser() {
		spinnerAdapter = new RecommendAdapter(this,
				recommendUsersModel.recommendList);
		title.setText("第" + (i + 1) + "代("
				+ recommendUsersModel.recommendList.size() + ")");
		title.setGravity(Gravity.CENTER);
		listView.setAdapter(spinnerAdapter);
		i++;
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url.endsWith(ProtocolConst.RECOMMEND_USERS)) {
			setUser();
		}
	}

}
